package com.kitty.game.fight.factory.model;

import com.kitty.common.utils.SpringUtils;
import com.kitty.common.utils.PositionUtils;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.FightObject;
import com.kitty.game.fight.bean.PetFightObject;
import com.kitty.game.fight.bean.PlayerFightObject;
import com.kitty.game.fight.message.RespCombatFriends;
import com.kitty.game.fight.message.RespCombatOpponets;
import com.kitty.game.fight.message.RespRefreshPetList;
import com.kitty.game.fight.message.vo.FightMember;
import com.kitty.game.fight.message.vo.RefreshPet;
import com.kitty.game.fight.service.FightExecutorService;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.team.model.Member;
import com.kitty.game.utils.Const;
import com.kitty.mina.message.MessagePusher;
import org.nutz.lang.util.NutMap;

import java.util.ArrayList;
import java.util.List;

public class TeamPkFight extends Fight {

    private List<Member> listA;
    private List<Member> listB;


    private ArrayList<FightMember> fightMemberA = new ArrayList<>();
    private ArrayList<FightMember> fightMemberB = new ArrayList<>();


    private static NutMap publicAutoSet = NutMap.NEW();

    static {
        publicAutoSet.setv("type", 2);
        publicAutoSet.setv("action", 1);
        publicAutoSet.setv("skillId", 2);
        publicAutoSet.setv("list", new ArrayList<>());
    }


    public TeamPkFight(List<Member> listA, List<Member> listB) {
        super(Const.fightType_PK);
        this.listA = listA;
        this.listB = listB;

        FightExecutorService.FIGHTS.put(getId(), this);
    }

    public TeamPkFight(int type, List<Member> listA, List<Member> listB) {
        super(type);
        this.listA = listA;
        this.listB = listB;

        FightExecutorService.FIGHTS.put(getId(), this);
    }


    protected void loadActors() {
        initRoleAndPetInfo(true);
        initRoleAndPetInfo(false);
    }

    protected void notifyAndStart() {
        sendToSide(Boolean.TRUE);
        sendToSide(Boolean.FALSE);

        SpringUtils.getFightService().whileSendStart(this);
        for (FightObject fightObject : getAllFightObjects()) {
            if (fightObject.isPlayer()) {
                SpringUtils.getFightService().autoFightOne(this, fightObject);
            }
        }
    }

    @Override
    public boolean isPkFight() {
        return true;
    }

    private void initRoleAndPetInfo(boolean isA) {
        int index = 0, petIndex = 5;

        List<Member> memberList;
        List<FightObject> fightObjectList;
        if (isA) {
            fightObjectList = getListA();
            memberList = listA;
        } else {
            fightObjectList =getListB();
            memberList = listB;
        }
        int customIndex =0;
        for (Member member : memberList) {
            if (!member.isInTeam()) {
                continue;
            }
            Role teamRole = SpringUtils.getRoleService().getOnlinePlayer(member.getUid());
            if (teamRole == null) {
                continue;
            }
            FightExecutorService.FIGHT_ROOMS.put(member.getRoleId(), getId());

            SpringUtils.getFightService().fullLifeRoleAndPet(teamRole);// 有储备情况下进战斗自动满血满蓝
            fightService.checkAtFightStart(teamRole);
            FightObject fightObject = new PlayerFightObject(teamRole, index,customIndex, isA);
            ((PlayerFightObject) fightObject).setUseMonthTao(isUseMonth());
            fightObjectList.add(fightObject);

            FightMember fightMember;
            if(isA){
                fightMember = fightObject.asFightMember(getCustomIconListA());
                fightMemberA.add(fightMember);
            }else {
                fightMember = fightObject.asFightMember(getCustomIconListB());
                fightMemberB.add(fightMember);
            }


            RespRefreshPetList respRefreshPetList = new RespRefreshPetList();
            RefreshPet refreshPet = new RefreshPet();
            ArrayList<RefreshPet> refreshPets = new ArrayList<>();
            refreshPets.add(refreshPet);
            refreshPet.setPetId(teamRole.getPetBox().getFightPetId());
            respRefreshPetList.setPetList(refreshPets);
            MessagePusher.pushMessage(teamRole, respRefreshPetList);
            ++index;
            ++customIndex;
            Pet pet = teamRole.getPetBox().getPetByPetId(teamRole.getPetBox().getFightPetId());
            if (pet != null && pet.canJoinFight()) {
                ++customIndex;
                PetFightObject petFightObj = new PetFightObject(pet, fightObject, PositionUtils.positions.get(petIndex));

                fightObjectList.add(petFightObj);

                FightMember petFightMember = petFightObj.asFightMember();
                if(isA){
                    fightMemberA.add(petFightMember);
                }else {
                    fightMemberB.add(petFightMember);
                }

                FightExecutorService.PETID_ROLEID.put(pet.getId(), member.getRoleId());
                FightExecutorService.FIGHT_ROOMS.put(pet.getId(), getId());
            }else if(pet!=null){
                MessagePusher.pushMessage(teamRole,new RespMsg("你当前参战宠物的#R忠诚度#n太低了，当宠物的忠诚小于等于70时有一定几率不参加战斗！"));
            }
            ++petIndex;
        }
    }


    private void sendToSide(boolean isA) {
        RespCombatFriends respCombatFriends = new RespCombatFriends();
        RespCombatOpponets respCombatOpponets = new RespCombatOpponets();
        List<Member> members;

        if (isA) {
            members = listA;
            respCombatFriends.setList(fightMemberA);
            respCombatFriends.setCustomIconList(getCustomIconListA());
            respCombatOpponets.setCustomIconList(getCustomIconListB());
            respCombatOpponets.setList(fightMemberB);
        } else {
            members = listB;
            respCombatFriends.setList(fightMemberB);
            respCombatFriends.setCustomIconList(getCustomIconListB());
            respCombatOpponets.setCustomIconList(getCustomIconListA());
            respCombatOpponets.setList(fightMemberA);
        }

        SpringUtils.getFightService().sendStartCombat(respCombatFriends, respCombatOpponets, members);
    }


}
